﻿#region Author informations
/*  __
 * | *\_
 * |  _ \
 * | |_| |
 * |____/
 * 
 * 
 * 
 * 
 */

#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections;
using System.Text.RegularExpressions;


using Safe_Driving.EasyCalendar.Model;
using Safe_Driving.TokenUser;

/**
 * Nom session formation
 * Moniteur
 * Nombre de place libre
 * début-fin
 * bande couleur moto/voiture et permis/code/perf session exam
 *
 * 
 */

namespace Safe_Driving.EasyCalendar.Controler
{
        

        public static class DateEventFilter
        {
            
            public static IEnumerable<EventCal> ByDate(this IEnumerable<EventCal> events, DateTime date)
            {
                SingletonTokenUser tok = SingletonTokenUser.GetInstance();
                AppSafeDriving dbApp = tok.DbApp;

                EventCal evt = new EventCal();
                ObservableCollection<EventCal> vect = new ObservableCollection<EventCal>();

                
                DateTime date_begin = DateTime.Parse(date.Year + "/" + date.Month + "/" + date.Day + " 07:00:00");
                DateTime date_end = DateTime.Parse(date.Year + "/" + date.Month + "/" + date.Day + " 22:00:00");
                date_end += TimeSpan.FromDays(7);

     
                switch (tok.Current_tab)
                {
                    //Tab indispo moniteur
                    case "indispo_Moniteur_TabItem":
                        foreach (SFInDiSpOnIbiLite indispo in (from sfIndispo in dbApp.SFInDiSpOnIbiLite
                                                               where sfIndispo.IDMonItEUR == tok.Moniteur_connected.IDMonItEUR
                                                                        && sfIndispo.DateFIn <= date_end 
                                                                        && sfIndispo.DateDebut >= date_begin
                                                               orderby sfIndispo.DateDebut
                                                               select sfIndispo))
                        {
                            vect.Add(createEvent(indispo.DateDebut, indispo.DateFIn, "indispo", indispo.IDInDiSpOnIbiLite));
                        }
                        
                        break;

                    case "cours_test":
                        foreach (SFCOurs cours in (from sfcours in dbApp.SFCOurs
                                                   where sfcours.DateFIn <= date_end
                                                         && sfcours.DateDebut >= date_begin
                                                   orderby sfcours.DateDebut
                                                   select sfcours))
                        {
                            vect.Add(createEvent(cours.DateDebut, cours.DateFIn, cours.NoM, cours.TypeCOurs, cours.IDCOurs, cours.CapAcItemAx));
                        }

                        break;

                    case "Reservation_formation_TabItem":

                        foreach (SFCOurs cours in (from sfcours in dbApp.SFCOurs
                                                   where sfcours.DateFIn <= date_end
                                                         && sfcours.DateDebut >= date_begin
                                                   orderby sfcours.DateDebut
                                                   select sfcours))
                        {
                            vect.Add(createEvent(cours.DateDebut, cours.DateFIn, cours.NoM, cours.TypeCOurs, cours.IDCOurs, cours.CapAcItemAx));
                        }



                        IEnumerator vectEnum = vect.GetEnumerator();
                        while (vectEnum.MoveNext())
                        {
                            ((EventCal)vectEnum.Current).Nb_inscrit = get_nbinscrits(((EventCal)vectEnum.Current).Id);

                            if (get_inscrit(((EventCal)vectEnum.Current).Id, tok.Client_selected.IDClient))
                            {
                                ((EventCal)vectEnum.Current).Inscrit = EventCal.OuiInscrit;
                            }
                            else
                            {
                                ((EventCal)vectEnum.Current).Inscrit = EventCal.NonInscrit;
                            }

                            if (((EventCal)vectEnum.Current).Nb_inscrit < ((EventCal)vectEnum.Current).Capacite)
                            {
                                ((EventCal)vectEnum.Current).Btn_visible = Visibility.Visible;
                                ((EventCal)vectEnum.Current).Couleur_dispo = EventCal.Cours_dispo;
                                ((EventCal)vectEnum.Current).Img_cross_visible = Visibility.Hidden;
                            }
                            else
                            {
                                if (((EventCal)vectEnum.Current).Inscrit == EventCal.OuiInscrit)
                                {
                                    ((EventCal)vectEnum.Current).Btn_visible = Visibility.Visible;
                                } else {
                                    ((EventCal)vectEnum.Current).Btn_visible = Visibility.Hidden;
                                }

                                ((EventCal)vectEnum.Current).Img_cross_visible = Visibility.Visible;
                                ((EventCal)vectEnum.Current).Couleur_dispo = EventCal.Cours_complet;
                            }
                        }
                

                        break;

                    default:
                        break;
                }

                return (IEnumerable<EventCal>)vect;
            }

            private static int get_nbinscrits(int id)
            {
                SingletonTokenUser tok = SingletonTokenUser.GetInstance();
                AppSafeDriving dbApp = tok.DbApp;

                int nb_inscrit = (from inscrits in dbApp.SFInSCrIt
                                                     where inscrits.IDCOurs == id
                                                     select inscrits).Count<SFInSCrIt>();
                return nb_inscrit;
            }

            private static bool get_inscrit(int id, int idclient)
            {
                SingletonTokenUser tok = SingletonTokenUser.GetInstance();
                AppSafeDriving dbApp = tok.DbApp;

                int nb_inscrit = (from inscrits in dbApp.SFInSCrIt
                                  where inscrits.IDCOurs == id && inscrits.IDClient == idclient
                                  select inscrits).Count<SFInSCrIt>();

                return nb_inscrit == 1;
            }
            
            public static EventCal createEvent(DateTime start, DateTime stop, string message, string type, int id, int capacite)
            {
                EventCal evt = new EventCal();
                evt.Capacite = capacite;
                evt.Id = id;
                evt.StartTime = start;
                evt.EndTime = stop;
                evt.Name_event = message;
                
                if (Regex.IsMatch(type, "auto"))
                {
                    evt.Type_session = EventCal.Voiture;
                    evt.Type = Regex.Replace(type, "auto", "");
                }
                else
                {
                    evt.Type_session = EventCal.Moto;
                    evt.Type = Regex.Replace(type, "moto", "");
                }
                
                return evt;
            }

            public static EventCal createEvent(DateTime start, DateTime stop, string message, int id)
            {
                EventCal evt = new EventCal();
                evt.StartTime = start;
                evt.EndTime = stop;
                evt.Name_event = message;
                evt.Id = id;
                return evt;
            }

        }

}
